using System.Collections.Generic;
using BasicScript;
using Game.Component;
using Game.Const;
using Game.Core;
using Game.Core.Loader;
using Game.Main.Controller;
using Game.Main.Event;
using Game.Main.Model;
using Game.Manager;
using UnityEngine;

namespace Game.Main.View
{
	public class Scene : Singleton<Scene>
	{
		public enum CameraMoveType
		{
			Static = 0,
			Interia = 1,
			Uniform = 2,
			Follow = 3
		}

		public enum StorageBoxMoveType
		{
			Static = 0,
			Follow = 1,
			Elastic = 2
		}

		public enum StorageBoxCameraMoveType
		{
			Static = 0,
			MoveRight = 1,
			MoveLeft = 2
		}

		private Map map;

		private StorageBox storageBox;

		private SceneModel model;

		private MapData mapData;

		private SunItem sun;

		private StarItem star;

		private List<BaseItem> items = new List<BaseItem>();

		private List<string> needLoadItemIds = new List<string>();

		private int needLoadResNum;

		private List<string> preLoadRess = new List<string>();

		private SceneController clr;

		private AudioManager audioManager;

		private CameraManager cameraManager;

		private UIManager uiManager;

		private DepthManager depthManager;

		private AssetBundleManager assetManager;

		private Dictionary<string, ResourceLoader> modelLoaderDic = new Dictionary<string, ResourceLoader>();

		private bool canTouch;

		private bool canPlay;

		private bool isLock;

		private bool isGameStart;

		private Light light;

		private float sunPercent;

		private float lampLight;

		private Dictionary<uint, float> openLamps = new Dictionary<uint, float>();

		private CameraMoveType cameraMoveType;

		private float cameraMoveV;

		private float cameraMoveMinArea;

		private float cameraMoveMaxArea;

		private StorageBoxCameraMoveType sbCameraMoveType;

		private StorageBoxMoveType sbMoveType;

		private const float sbMoveSpeed = 4f;

		private const float sbOpenX = 2f;

		private const float sbCloseX = 0f;

		private float sbMoveV;

		private float targetSBY;

		private int clrStorageBoxTouchId = -1;

		private bool isClrStorageBoxTouchExist;

		private bool isSBOpen;

		private Touch touch;

		private TouchEventData[] touchEvtDatas = new TouchEventData[12]
		{
			new TouchEventData(0),
			new TouchEventData(1),
			new TouchEventData(2),
			new TouchEventData(3),
			new TouchEventData(4),
			new TouchEventData(5),
			new TouchEventData(6),
			new TouchEventData(7),
			new TouchEventData(8),
			new TouchEventData(9),
			new TouchEventData(10),
			new TouchEventData(11)
		};

		private RaycastHit2D hit;

		private RaycastHit2D[] hits;

		private int clrCameraTouchId = -1;

		private bool isClrCameraTouchExist;

		private float targetCameraX = float.MaxValue;

		private float lastX;

		public Scene()
		{
			model = Singleton<SceneModel>.Instance;
			audioManager = Singleton<AudioManager>.Instance;
			cameraManager = Singleton<CameraManager>.Instance;
			uiManager = Singleton<UIManager>.Instance;
			depthManager = Singleton<DepthManager>.Instance;
			assetManager = Singleton<AssetBundleManager>.Instance;
			light = GameObject.Find("Light").GetComponent<Light>();
			cameraMoveMinArea = (float)Screen.width * 0.2f;
			cameraMoveMaxArea = (float)Screen.width * 0.8f;
		}

		public void SetController(SceneController controller)
		{
			clr = controller;
		}

		public void InitMap(MapData data)
		{
			if (map == null)
			{
				map = new Map();
			}
			InitData();
			mapData = data;
			map.mapId = mapData.id;
			map.Bind(LoadEvent.LOAD_COMPLETE, OnMapLoadComplete);
			map.Init();
		}

		private void InitData()
		{
			canTouch = false;
			isGameStart = false;
			canPlay = false;
			isLock = false;
			isSBOpen = false;
			sunPercent = 0f;
			lampLight = 0f;
			needLoadResNum = 0;
			clrCameraTouchId = -1;
			clrStorageBoxTouchId = -1;
			cameraMoveType = CameraMoveType.Static;
			sbCameraMoveType = StorageBoxCameraMoveType.Static;
			foreach (KeyValuePair<string, ResourceLoader> item in modelLoaderDic)
			{
				item.Value.Destroy();
			}
			depthManager.Clear();
			needLoadItemIds.Clear();
			modelLoaderDic.Clear();
			model.InitData();
			preLoadRess.Clear();
			items.Clear();
			openLamps.Clear();
		}

		private void OnMapLoadComplete(BaseEvent evt)
		{
			assetManager.Bind(LoadEvent.LOAD_COMPLETE, OnCharacterResLoadComplete);
			assetManager.LoadCharacterResources();
		}

		private void OnCharacterResLoadComplete(BaseEvent evt)
		{
			assetManager.UnBind(LoadEvent.LOAD_COMPLETE, OnCharacterResLoadComplete);
			model.LoadArchiveData(map.mapId);
			if (storageBox == null)
			{
				storageBox = new StorageBox();
				storageBox.Bind(LoadEvent.LOAD_COMPLETE, OnStorageBoxLoadComplete);
				storageBox.Init();
            }
			else
			{
				MapLoadComplete();
			}
		}

		private void OnStorageBoxLoadComplete(BaseEvent evt)
		{
			storageBox.UnBind(LoadEvent.LOAD_COMPLETE, OnStorageBoxLoadComplete);
			MapLoadComplete();
		}

		private void MapLoadComplete()
		{
			cameraManager.SetBorder(map.sceneLayerBorder);
			cameraManager.ResetPosition();
			depthManager.Init(new DepthSystem(map.sceneLayer), new DepthSystem(map.distLayer), new DepthSystem(storageBox.storageBox));
			map.UnBind(LoadEvent.LOAD_COMPLETE, OnMapLoadComplete);
			storageBox.SetScreenWidth(cameraManager.wSize);
			LoadModel();
		}

		private void LoadModel()
		{
			if (model.itemArchiveData.Count != 0)
			{
				ClearItem(map.sceneLayerItem);
				ClearItem(map.distLayerItem);
				for (int i = 0; i < model.itemArchiveData.Count; i++)
				{
					AddModelLoader(model.itemArchiveData[i].itemData, string.Empty);
				}
			}
			else
			{
				AddItemModelLoader(map.sceneLayerItem, 0);
				AddItemModelLoader(map.distLayerItem, 1);
			}
			if (model.isSBArchiveExist)
			{
				ClearItem(storageBox.itemTrans);
				for (int j = 0; j < model.storageboxData.Count; j++)
				{
					AddModelLoader(model.storageboxData[j].itemData, string.Empty);
				}
			}
			else
			{
				AddItemModelLoader(storageBox.itemTrans, 9);
			}
			bool flag = false;
			bool flag2 = false;
			for (int k = 0; k < model.itemArchiveData.Count; k++)
			{
				if (!flag && model.itemArchiveData[k].itemData.scriptType == 73)
				{
					flag = true;
				}
				if (!flag2 && model.itemArchiveData[k].itemData.scriptType == 153)
				{
					flag2 = true;
				}
				if (flag2 && flag)
				{
					break;
				}
			}
			for (int num = model.storageboxData.Count - 1; num >= 0; num--)
			{
				ItemArchiveData itemArchiveData = model.storageboxData[num];
				if (itemArchiveData.mapId == mapData.id || (flag && itemArchiveData.mapId == 101) || (flag2 && itemArchiveData.mapId == 102))
				{
					itemArchiveData.layer = 0;
					model.storageboxData.RemoveAt(num);
					model.itemArchiveData.Add(itemArchiveData);
				}
				else
				{
					itemArchiveData.layer = 9;
				}
			}
			if (flag)
			{
				for (int l = 0; l < model.elevatorData.Count; l++)
				{
					AddModelLoader(model.elevatorData[l].itemData, string.Empty);
				}
			}
			if (flag2)
			{
				if (model.ambulanceData.Count <= 0)
				{
					ResourceLoader resourceLoader = new ResourceLoader("prefabs/map/ambulance", "ambulance.prefab");
					resourceLoader.Bind(LoadEvent.LOAD_COMPLETE, OnAmulanceLoadComplete);
					resourceLoader.Load();
					return;
				}
				for (int m = 0; m < model.ambulanceData.Count; m++)
				{
					AddModelLoader(model.ambulanceData[m].itemData, string.Empty);
				}
			}
			StartLoadResources();
		}

		private void OnAmulanceLoadComplete(BaseEvent evt)
		{
			ResourceLoader resourceLoader = evt.sender as ResourceLoader;
			Transform transform = Object.Instantiate((GameObject)resourceLoader.resource).transform;
			AddItemModelLoader(transform, 10);
			Object.Destroy(transform.gameObject);
			resourceLoader.Destroy();
			StartLoadResources();
		}

		private void StartLoadResources()
		{
			foreach (KeyValuePair<string, ResourceLoader> item in modelLoaderDic)
			{
				item.Value.Load();
				needLoadResNum++;
			}
		}

		private void AddItemModelLoader(Transform trans, int layer)
		{
			int childCount = trans.childCount;
			for (int num = childCount - 1; num >= 0; num--)
			{
				Transform child = trans.GetChild(num);
				ItemData itemData = null;
				ItemArchiveData itemArchiveData = null;
				int mapId = 0;
				switch (layer)
				{
				case 9:
				{
					string[] array = child.name.Split(',');
					itemData = model.GetItemData(array[0]);
					if (itemData != null)
					{
						mapId = int.Parse(array[1]);
						itemArchiveData = BaseItem.CreateBasicData(itemData, layer, float.Parse(array[2]), float.Parse(array[3]), true);
						model.storageboxData.Add(itemArchiveData);
						itemArchiveData.index = uint.Parse(itemData.id.Replace("character", string.Empty));
						itemArchiveData.mapId = mapId;
					}
					break;
				}
				case 10:
					itemData = model.GetItemData(child.name);
					if (itemData != null)
					{
						mapId = mapData.id;
						itemArchiveData = BaseItem.CreateBasicData(itemData, 0, child.position.x, child.position.y, true);
						itemArchiveData.index = model.GenerateItemIndex();
						itemArchiveData.localX = child.localPosition.x;
						itemArchiveData.localY = child.localPosition.y;
						itemArchiveData.mapId = 102;
						model.ambulanceData.Add(itemArchiveData);
					}
					break;
				default:
					itemData = model.GetItemData(child.name);
					if (itemData != null)
					{
						mapId = mapData.id;
						itemArchiveData = BaseItem.CreateBasicData(itemData, layer, child.position.x, child.position.y, true);
						itemArchiveData.index = model.GenerateItemIndex();
						itemArchiveData.mapId = mapId;
						model.itemArchiveData.Add(itemArchiveData);
					}
					break;
				}
				if (itemData == null)
				{
					Debug.LogError("ID为" + child.name + "的物件不存在");
				}
				else
				{
					AddModelLoader(itemData, string.Empty);
					AddChildItemModelLoader(itemArchiveData, child, layer, mapId);
				}
				Object.Destroy(child.gameObject);
			}
		}

		private void AddChildItemModelLoader(ItemArchiveData parentItem, Transform parentTrans, int layer, int mapId)
		{
			int childCount = parentTrans.childCount;
			for (int i = 0; i < childCount; i++)
			{
				Transform child = parentTrans.GetChild(i);
				string[] array = child.name.Split(',');
				bool flag = false;
				ItemData itemData = null;
				if (array[0].StartsWith("C"))
				{
					flag = true;
					itemData = model.GetItemData(array[0].Substring(1));
				}
				else
				{
					itemData = model.GetItemData(array[0]);
				}
				if (array.Length == 3 && itemData != null)
				{
					ItemArchiveData itemArchiveData = null;
					if (layer == 10)
					{
						if (flag)
						{
							itemArchiveData = model.GetItemArchiveData(itemData.id);
						}
						if (itemArchiveData == null)
						{
							itemArchiveData = BaseItem.CreateBasicData(itemData, 0, child.position.x, child.position.y, true);
							itemArchiveData.localX = child.localPosition.x;
							itemArchiveData.localY = child.localPosition.y;
							itemArchiveData.mapId = 102;
							itemArchiveData.layer = 0;
						}
						else if (itemArchiveData.mapId == 0)
						{
							itemArchiveData.localX = child.localPosition.x;
							itemArchiveData.localY = child.localPosition.y;
							itemArchiveData.mapId = 102;
							itemArchiveData.layer = 0;
						}
					}
					else
					{
						itemArchiveData = BaseItem.CreateBasicData(itemData, layer, child.position.x, child.position.y, true);
						itemArchiveData.mapId = mapId;
					}
					if (flag)
					{
						itemArchiveData.index = uint.Parse(itemData.id.Replace("character", string.Empty));
					}
					else
					{
						itemArchiveData.index = model.GenerateItemIndex();
						switch (layer)
						{
						case 9:
							model.storageboxData.Add(itemArchiveData);
							break;
						case 10:
							model.ambulanceData.Add(itemArchiveData);
							break;
						default:
							model.itemArchiveData.Add(itemArchiveData);
							break;
						}
					}
					if (array[1].Equals("front"))
					{
						if (parentItem.frontNodes == null)
						{
							parentItem.frontNodes = new List<ChildNodeData>();
						}
						parentItem.frontNodes.Add(new ChildNodeData(itemArchiveData.index, int.Parse(array[2])));
					}
					else if (array[1].Equals("ahead"))
					{
						if (parentItem.aheadNodes == null)
						{
							parentItem.aheadNodes = new List<ChildNodeData>();
						}
						parentItem.aheadNodes.Add(new ChildNodeData(itemArchiveData.index, int.Parse(array[2])));
					}
					else if (array[1].Equals("behind"))
					{
						if (parentItem.behindNodes == null)
						{
							parentItem.behindNodes = new List<ChildNodeData>();
						}
						parentItem.behindNodes.Add(new ChildNodeData(itemArchiveData.index, int.Parse(array[2])));
					}
					else if (array[1].Equals("back"))
					{
						if (parentItem.backNodes == null)
						{
							parentItem.backNodes = new List<ChildNodeData>();
						}
						parentItem.backNodes.Add(new ChildNodeData(itemArchiveData.index, int.Parse(array[2])));
					}
					if (!flag)
					{
						AddChildItemModelLoader(itemArchiveData, child, layer, mapId);
						AddModelLoader(itemData, string.Empty);
					}
				}
				else
				{
					Debug.LogError("ID为" + child.name + "的物件不存在");
				}
			}
		}

		private void ClearItem(Transform trans)
		{
			int childCount = trans.childCount;
			for (int num = childCount - 1; num >= 0; num--)
			{
				Object.DestroyImmediate(trans.GetChild(num).gameObject);
			}
		}

		private void AddModelLoader(ItemData data, string id = "")
		{
			if (data == null)
			{
				if (needLoadItemIds.Contains(id))
				{
					return;
				}
				data = model.GetItemData(id);
				if (data == null)
				{
					Trace.LogError("物件ID为" + id + "的物件不存在");
				}
			}
			else if (needLoadItemIds.Contains(data.id))
			{
				return;
			}
			needLoadItemIds.Add(data.id);
			if (!modelLoaderDic.ContainsKey(data.model))
			{
				string path = PathConst.MODEL + data.model;
				ResourceLoader resourceLoader = new ResourceLoader(path, data.model + ".prefab", true);
				resourceLoader.Bind(LoadEvent.LOAD_COMPLETE, OnModelLoadComplete);
				modelLoaderDic.Add(data.model, resourceLoader);
			}
			if (data.createIds != null)
			{
				for (int i = 0; i < data.createIds.Length; i++)
				{
					AddModelLoader(null, data.createIds[i]);
				}
			}
			if (!string.IsNullOrEmpty(data.changeId))
			{
				AddModelLoader(null, data.changeId);
			}
			if (!string.IsNullOrEmpty(data.effect))
			{
				AddPreLoadResource(PathConst.EFFECT + data.effect);
			}
			if (!string.IsNullOrEmpty(data.dragEfc))
			{
				AddPreLoadResource(PathConst.EFFECT + data.dragEfc);
			}
			List<ComposeData> composeData = model.GetComposeData(data.id);
			if (composeData != null)
			{
				data.composeData = composeData;
				for (int j = 0; j < composeData.Count; j++)
				{
					AddModelLoader(null, composeData[j].id3);
				}
			}
		}

		private void AddPreLoadResource(string path)
		{
			if (!preLoadRess.Contains(path))
			{
				preLoadRess.Add(path);
			}
		}

		private void OnModelLoadComplete(BaseEvent evt)
		{
			needLoadResNum--;
			if (needLoadResNum <= 0)
			{
				needLoadItemIds.Clear();
				LoadResources();
			}
		}

		private void LoadResources()
		{
            AddPreLoadResource(PathConst.EFFECT + "efc_water_wave");
			AddPreLoadResource(PathConst.EFFECT + "efc_water_fall");
			AddPreLoadResource(PathConst.EFFECT + "efc_explode_1");
			AddPreLoadResource(PathConst.EFFECT + "efc_blink_1");
			needLoadResNum = preLoadRess.Count;
			for (int i = 0; i < preLoadRess.Count; i++)
			{
                ResourceLoader resourceLoader = new ResourceLoader(preLoadRess[i], preLoadRess[i].Substring(preLoadRess[i].LastIndexOf("/") + 1) + ".prefab", true);
				resourceLoader.Bind(LoadEvent.LOAD_COMPLETE, OnResLoadComplete);
                resourceLoader.Load();
            }
		}

        private void OnResLoadComplete(BaseEvent evt)
		{
			needLoadResNum--;
			ResourceLoader resourceLoader = evt.sender as ResourceLoader;
			Singleton<GamePoolManager>.Instance.InitPrefabPool(resourceLoader.path, ((GameObject)resourceLoader.resource).transform, 1, 50, 1, 10);
			resourceLoader.Destroy();
			if (needLoadResNum <= 0)
			{
				cameraManager.SetPositionX(model.cameraX);
				InitItems();
				Singleton<AssetBundleManager>.Instance.ClearAssets(false);
				clr.OnSceneInitComplete(null);
				isGameStart = true;
				if (map.mapId == 1)
				{
					canPlay = true;
				}
				else
				{
					canPlay = Singleton<SaveManager>.Instance.IsUnlock;
				}
				clr.SetLockMaskActive(!canPlay);
				isLock = !Singleton<SaveManager>.Instance.IsUnlock;
				storageBox.IsLock(isLock);
				//
				UnlockScene();
                storageBox.IsLock(true);
            }
		}

		public void UnlockScene()
		{
			canPlay = true;
			isLock = false;
			if (storageBox != null)
			{
				storageBox.IsLock(false);
			}
		}

		private void InitItems()
		{
			bool flag = false;
			bool flag2 = false;
			for (int i = 0; i < model.itemArchiveData.Count; i++)
			{
				ItemArchiveData itemArchiveData = model.itemArchiveData[i];
				if (!flag && itemArchiveData.itemData.scriptType == 73)
				{
					flag = true;
				}
				if (!flag2 && itemArchiveData.itemData.scriptType == 153)
				{
					flag2 = true;
				}
				InitItem(itemArchiveData, false, true);
			}
			for (int j = 0; j < model.storageboxData.Count; j++)
			{
				ItemArchiveData archiveData = model.storageboxData[j];
				InitItem(archiveData, false, true);
			}
			if (flag)
			{
				for (int k = 0; k < model.elevatorData.Count; k++)
				{
					ItemArchiveData archiveData2 = model.elevatorData[k];
					InitItem(archiveData2, false, true);
				}
			}
			if (flag2)
			{
				for (int l = 0; l < model.ambulanceData.Count; l++)
				{
					ItemArchiveData archiveData3 = model.ambulanceData[l];
					InitItem(archiveData3, false, true);
				}
			}
			for (int m = 0; m < items.Count; m++)
			{
				BaseItem baseItem = items[m];
				AttachChildNode(baseItem, baseItem.archiveData.frontNodes, DepthType.Front);
				AttachChildNode(baseItem, baseItem.archiveData.aheadNodes, DepthType.Ahead);
				AttachChildNode(baseItem, baseItem.archiveData.behindNodes, DepthType.Behind);
				AttachChildNode(baseItem, baseItem.archiveData.backNodes, DepthType.Back);
			}
			for (int n = 0; n < items.Count; n++)
			{
				BaseItem baseItem2 = items[n];
				if (model.isEmptyScene && baseItem2.parentItem == null && baseItem2.itemData.placeType != 0)
				{
					baseItem2.PutBackPlatform();
				}
				baseItem2.ResetState();
			}
			if (sun == null || sun.state == SunItem.State.Sun)
			{
				ChangeSunLight(1f);
			}
			else
			{
				ChangeSunLight(0f);
				SetSunMoonEnviroment(false);
			}
			BaseItem itemByScriptType = GetItemByScriptType(73);
			BaseItem itemByScriptType2 = GetItemByScriptType(153);
			if (itemByScriptType != null || itemByScriptType2 != null)
			{
				for (int num = 0; num < items.Count; num++)
				{
					BaseItem baseItem3 = items[num];
					if (baseItem3 != null)
					{
						if (itemByScriptType != null && baseItem3.archiveData.mapId == 101 && baseItem3.parentItem == null)
						{
							itemByScriptType.AttachItem(baseItem3, DepthType.Behind, BaseItem.AttachType.Place, false);
							baseItem3.SetLocalPosition(baseItem3.archiveData.localX, baseItem3.archiveData.localY);
						}
						if (itemByScriptType2 != null && baseItem3.archiveData.mapId == 102 && baseItem3.parentItem == null)
						{
							itemByScriptType2.AttachItem(baseItem3, DepthType.Behind, BaseItem.AttachType.Place, false);
							baseItem3.SetLocalPosition(baseItem3.archiveData.localX, baseItem3.archiveData.localY);
						}
					}
				}
				storageBox.UpdateStorageBoxItemPosition(true);
				if (model.isElevatorTransfer)
				{
					if (itemByScriptType != null)
					{
						cameraManager.SetPositionX(itemByScriptType.transform.position.x);
					}
					((ElevatorItem)itemByScriptType).SetState(ElevatorItem.State.Open);
					model.isElevatorTransfer = false;
				}
				if (model.isAmbulanceTransfer)
				{
					if (itemByScriptType2 != null)
					{
						cameraManager.SetPositionX(itemByScriptType2.transform.position.x);
						((AmbulanceItem)itemByScriptType2).MoveToPoint0();
					}
					model.isAmbulanceTransfer = false;
				}
			}
			storageBox.InitStorageBoxItems();
			depthManager.SortSceneLayer(true);
			depthManager.SortDistLayer(true);
			depthManager.SortStorageBoxLayer(true);
		}

		public void AttachChildNode(BaseItem item, List<ChildNodeData> childNodes, DepthType depthType)
		{
			if (childNodes == null)
			{
				return;
			}
			for (int i = 0; i < childNodes.Count; i++)
			{
				BaseItem item2 = GetItem(childNodes[i].index);
				if (item2 != null)
				{
					if (item2.archiveData.layer == item.archiveData.layer && item2.archiveData.mapId == item.archiveData.mapId)
					{
						item.AttachItem(item2, depthType, (BaseItem.AttachType)childNodes[i].attachType, true);
					}
				}
				else
				{
					Debug.LogWarning(item.itemData.id + "物件的子节点丢失：" + childNodes[i].index);
				}
			}
		}

		public BaseItem ChangeItem(BaseItem item, string id)
		{
			if (item.childItems.Count > 0)
			{
				Trace.LogError(item.archiveData.index + "含有子节点数据，无法改变物件");
				return null;
			}
			ItemData itemData = model.GetItemData(id);
			if (itemData != null)
			{
				ItemArchiveData archiveData = item.archiveData;
				archiveData.itemData = itemData;
				return InitItem(archiveData, true);
			}
			return null;
		}

		public BaseItem CreateItem(string id, Vector2 pos, int layer)
		{
			ItemData itemData = model.GetItemData(id);
			if (itemData != null)
			{
				if (modelLoaderDic.ContainsKey(itemData.model))
				{
					ItemArchiveData archiveData = BaseItem.CreateBasicData(itemData, layer, pos.x, pos.y, false);
					return InitItem(archiveData, false);
				}
				Trace.LogError("模型" + itemData.model + "未预加载");
			}
			return null;
		}

		private BaseItem InitItem(ItemArchiveData archiveData, bool isChange, bool isGameInit = false)
		{
			ItemData itemData = archiveData.itemData;
			if (!modelLoaderDic.ContainsKey(itemData.model))
			{
				Trace.LogError("不存在该模型 " + itemData.id + " " + itemData.model);
				return null;
			}
			if (archiveData.index == 0)
			{
				archiveData.index = model.GenerateItemIndex();
			}
			BaseItem baseItem = null;
			Transform transform = ((GameObject)Object.Instantiate(modelLoaderDic[itemData.model].resource)).transform;
			transform.name = archiveData.index + "." + itemData.id + "." + itemData.model;
			Rigidbody2D component = transform.gameObject.GetComponent<Rigidbody2D>();
			if (itemData.canDrag)
			{
				if (component == null)
				{
					component = transform.gameObject.AddComponent<Rigidbody2D>();
					component.bodyType = RigidbodyType2D.Kinematic;
				}
			}
			else if (component != null)
			{
				Object.Destroy(component);
			}
			baseItem = ((itemData.scriptType == 1) ? transform.gameObject.AddComponent<CharacterItem>() : ((itemData.scriptType == 36) ? transform.gameObject.AddComponent<CupItem>() : ((itemData.scriptType == 37) ? transform.gameObject.AddComponent<WaterDispenserItem>() : ((itemData.scriptType == 38) ? transform.gameObject.AddComponent<CreateItemMachineItem>() : ((itemData.scriptType == 52) ? transform.gameObject.AddComponent<ATMItem>() : ((itemData.scriptType == 65) ? transform.gameObject.AddComponent<VendingMachineItem>() : ((itemData.scriptType == 66) ? transform.gameObject.AddComponent<VendingMachineBoxItem>() : ((itemData.scriptType == 94) ? transform.gameObject.AddComponent<DefibrillatorItem>() : ((itemData.scriptType == 95) ? transform.gameObject.AddComponent<RopeItem>() : ((itemData.scriptType == 96) ? transform.gameObject.AddComponent<BreathingMachineItem>() : ((itemData.scriptType == 97) ? transform.gameObject.AddComponent<QueuingMachineItem>() : ((itemData.scriptType == 98) ? transform.gameObject.AddComponent<NumberCardItem>() : ((itemData.scriptType == 99) ? transform.gameObject.AddComponent<CallNumberItem>() : ((itemData.scriptType == 100) ? transform.gameObject.AddComponent<HeightMachineItem>() : ((itemData.scriptType == 101) ? transform.gameObject.AddComponent<CopyingMachineItem>() : ((itemData.scriptType == 93) ? transform.gameObject.AddComponent<BeverageItem>() : ((itemData.scriptType == 83) ? transform.gameObject.AddComponent<WindBellItem>() : ((itemData.scriptType == 51) ? transform.gameObject.AddComponent<DragModelItem>() : ((itemData.scriptType == 14) ? transform.gameObject.AddComponent<ClothingItem>() : ((itemData.scriptType == 21) ? transform.gameObject.AddComponent<FanItem>() : ((itemData.scriptType == 102) ? transform.gameObject.AddComponent<WashingMachineItem>() : ((itemData.scriptType == 103) ? transform.gameObject.AddComponent<ItemCreatorItem>() : ((itemData.scriptType == 104) ? transform.gameObject.AddComponent<FlowerpotItem>() : ((itemData.scriptType == 105) ? transform.gameObject.AddComponent<WateringPotItem>() : ((itemData.scriptType == 106) ? transform.gameObject.AddComponent<DefibrillatorDisplayerItem>() : ((itemData.scriptType == 107) ? transform.gameObject.AddComponent<RotateItem>() : ((itemData.scriptType == 108) ? transform.gameObject.AddComponent<EyeMachineDisplayerItem>() : ((itemData.scriptType == 109) ? transform.gameObject.AddComponent<EyeMachineItem>() : ((itemData.scriptType == 110) ? transform.gameObject.AddComponent<EyeMachineBoxItem>() : ((itemData.scriptType == 171) ? transform.gameObject.AddComponent<EyeTestToolItem>() : ((itemData.scriptType == 111) ? transform.gameObject.AddComponent<BodyScannerItem>() : ((itemData.scriptType == 112) ? transform.gameObject.AddComponent<BodyScannerDisplayerItem>() : ((itemData.scriptType == 114) ? transform.gameObject.AddComponent<ToothMachineDisplayerItem>() : ((itemData.scriptType == 113) ? transform.gameObject.AddComponent<ToothToolItem>() : ((itemData.scriptType == 115) ? transform.gameObject.AddComponent<SelectCreateItemItem>() : ((itemData.scriptType == 116) ? transform.gameObject.AddComponent<NeedleTubingItem>() : ((itemData.scriptType == 117) ? transform.gameObject.AddComponent<SitLieBeDItem>() : ((itemData.scriptType == 118) ? transform.gameObject.AddComponent<BodyDataItem>() : ((itemData.scriptType == 7) ? transform.gameObject.AddComponent<ContainerItem>() : ((itemData.scriptType == 80) ? transform.gameObject.AddComponent<ToothBrushItem>() : ((itemData.scriptType == 77) ? transform.gameObject.AddComponent<ClickChangerItem>() : ((itemData.scriptType == 32) ? transform.gameObject.AddComponent<PackingBoxItem>() : ((itemData.scriptType == 32) ? transform.gameObject.AddComponent<PackingBoxItem>() : ((itemData.scriptType == 119) ? transform.gameObject.AddComponent<StethoscopeItem>() : ((itemData.scriptType == 89) ? transform.gameObject.AddComponent<WaterBottleItem>() : ((itemData.scriptType == 120) ? transform.gameObject.AddComponent<ThermometerItem>() : ((itemData.scriptType == 121) ? transform.gameObject.AddComponent<TouchMoveItem>() : ((itemData.scriptType == 122) ? transform.gameObject.AddComponent<BandageItem>() : ((itemData.scriptType == 123) ? transform.gameObject.AddComponent<BabyItem>() : ((itemData.scriptType == 124) ? transform.gameObject.AddComponent<OxygenMachineItem>() : ((itemData.scriptType == 125) ? transform.gameObject.AddComponent<OperationBedItem>() : ((itemData.scriptType == 127) ? transform.gameObject.AddComponent<OperationDisplayerItem>() : ((itemData.scriptType == 126) ? transform.gameObject.AddComponent<OperationMachineItem>() : ((itemData.scriptType == 128) ? transform.gameObject.AddComponent<CTItem>() : ((itemData.scriptType == 129) ? transform.gameObject.AddComponent<CTBedItem>() : ((itemData.scriptType == 75) ? transform.gameObject.AddComponent<ClockAItem>() : ((itemData.scriptType == 130) ? transform.gameObject.AddComponent<ClockBItem>() : ((itemData.scriptType == 131) ? transform.gameObject.AddComponent<DynamoItem>() : ((itemData.scriptType == 132) ? transform.gameObject.AddComponent<ElementCreatorItem>() : ((itemData.scriptType == 133) ? transform.gameObject.AddComponent<ElectricMachineItem>() : ((itemData.scriptType == 134) ? transform.gameObject.AddComponent<MedicineMachineItem>() : ((itemData.scriptType == 135) ? transform.gameObject.AddComponent<TestTubeItem>() : ((itemData.scriptType == 136) ? transform.gameObject.AddComponent<MedicineConveyorItem>() : ((itemData.scriptType == 137) ? transform.gameObject.AddComponent<LinkRopeItem>() : ((itemData.scriptType == 138) ? transform.gameObject.AddComponent<MedicinePackerItem>() : ((itemData.scriptType == 140) ? transform.gameObject.AddComponent<BMachineItem>() : ((itemData.scriptType == 139) ? transform.gameObject.AddComponent<POSItem>() : ((itemData.scriptType == 141) ? transform.gameObject.AddComponent<CreditCardItem>() : ((itemData.scriptType == 20) ? transform.gameObject.AddComponent<PushPullCabinetItem>() : ((itemData.scriptType == 2) ? transform.gameObject.AddComponent<PaintBoardItem>() : ((itemData.scriptType == 4) ? transform.gameObject.AddComponent<BoardEraserItem>() : ((itemData.scriptType == 142) ? transform.gameObject.AddComponent<PaintBrushItem>() : ((itemData.scriptType == 143) ? transform.gameObject.AddComponent<BMachineDisplayerItem>() : ((itemData.scriptType == 144) ? transform.gameObject.AddComponent<FeederItem>() : ((itemData.scriptType == 145) ? transform.gameObject.AddComponent<KettleItem>() : ((itemData.scriptType == 146) ? transform.gameObject.AddComponent<MoundItem>() : ((itemData.scriptType == 147) ? transform.gameObject.AddComponent<GrowPlantItem>() : ((itemData.scriptType == 148) ? transform.gameObject.AddComponent<SprinklerItem>() : ((itemData.scriptType == 64) ? transform.gameObject.AddComponent<ShowerNozzleItem>() : ((itemData.scriptType == 40) ? transform.gameObject.AddComponent<WaterItem>() : ((itemData.scriptType == 55) ? transform.gameObject.AddComponent<ClosestoolItem>() : ((itemData.scriptType == 23) ? transform.gameObject.AddComponent<KiddieRideItem>() : ((itemData.scriptType == 149) ? transform.gameObject.AddComponent<BabyBedItem>() : ((itemData.scriptType == 150) ? transform.gameObject.AddComponent<SwimRingItem>() : ((itemData.scriptType == 73) ? transform.gameObject.AddComponent<ElevatorItem>() : ((itemData.scriptType == 151) ? transform.gameObject.AddComponent<SwingItem>() : ((itemData.scriptType == 152) ? transform.gameObject.AddComponent<Face1aSprayItem>() : ((itemData.scriptType == 44) ? transform.gameObject.AddComponent<TreasureItem>() : ((itemData.scriptType == 42) ? transform.gameObject.AddComponent<TreasureKeyItem>() : ((itemData.scriptType == 43) ? transform.gameObject.AddComponent<TreasureKeyBoxItem>() : ((itemData.scriptType == 153) ? transform.gameObject.AddComponent<AmbulanceItem>() : ((itemData.scriptType == 154) ? transform.gameObject.AddComponent<AmbulanceContainerItem>() : ((itemData.scriptType == 155) ? transform.gameObject.AddComponent<TelephoneBoothPhoneItem>() : ((itemData.scriptType == 156) ? transform.gameObject.AddComponent<StretcherItem>() : ((itemData.scriptType == 157) ? transform.gameObject.AddComponent<RideItem>() : ((itemData.scriptType == 160) ? transform.gameObject.AddComponent<InfusionBagItem>() : ((itemData.scriptType == 161) ? transform.gameObject.AddComponent<InfusionBraceletItem>() : ((itemData.scriptType == 159) ? transform.gameObject.AddComponent<InfusionSupportItem>() : ((itemData.scriptType == 84) ? transform.gameObject.AddComponent<TVItem>() : ((itemData.scriptType == 58) ? transform.gameObject.AddComponent<FaucetItem>() : ((itemData.scriptType == 49) ? transform.gameObject.AddComponent<TrashcanItem>() : ((itemData.scriptType == 162) ? transform.gameObject.AddComponent<TissueBoxBItem>() : ((itemData.scriptType == 163) ? transform.gameObject.AddComponent<BabyClothingItem>() : ((itemData.scriptType == 164) ? transform.gameObject.AddComponent<ElevatorButtonItem>() : ((itemData.scriptType == 165) ? transform.gameObject.AddComponent<StethoscopeBoxItem>() : ((itemData.scriptType == 85) ? transform.gameObject.AddComponent<ClickAnimChangerItem>() : ((itemData.scriptType == 166) ? transform.gameObject.AddComponent<AlertorItem>() : ((itemData.scriptType == 62) ? transform.gameObject.AddComponent<SuitcaseItem>() : ((itemData.scriptType == 79) ? transform.gameObject.AddComponent<ClickShowItem>() : ((itemData.scriptType == 167) ? transform.gameObject.AddComponent<ThermometerBItem>() : ((itemData.scriptType == 168) ? transform.gameObject.AddComponent<TrashcanBItem>() : ((itemData.scriptType == 76) ? transform.gameObject.AddComponent<LampItem>() : ((itemData.scriptType == 30) ? transform.gameObject.AddComponent<FoodItem>() : ((itemData.scriptType == 169) ? transform.gameObject.AddComponent<DoubleDoorCabinetItem>() : ((itemData.scriptType == 15) ? transform.gameObject.AddComponent<ShoesItem>() : ((itemData.scriptType == 170) ? transform.gameObject.AddComponent<FaceMaskItem>() : ((itemData.scriptType == 87) ? transform.gameObject.AddComponent<TVBItem>() : ((itemData.scriptType == 86) ? transform.gameObject.AddComponent<SpeakerItem>() : ((itemData.scriptType == 172) ? transform.gameObject.AddComponent<OperationLampItem>() : ((itemData.scriptType == 173) ? transform.gameObject.AddComponent<OperationECGItem>() : ((itemData.scriptType == 174) ? transform.gameObject.AddComponent<EletricBrakeItem>() : ((itemData.scriptType == 175) ? transform.gameObject.AddComponent<GemBoxItem>() : ((itemData.scriptType == 176) ? transform.gameObject.AddComponent<BreathingMaskItem>() : ((itemData.scriptType != 177) ? transform.gameObject.AddComponent<BaseItem>() : transform.gameObject.AddComponent<AmbulanceLampItem>()))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))));
			baseItem.Init(archiveData);
			SetItemParent(baseItem);
			baseItem.SetLocalScale(new Vector3(itemData.scale, itemData.scale, 1f));
			baseItem.SetPosition(archiveData.x, archiveData.y);
			BaseItem item = GetItem(archiveData.index);
			if (item != null)
			{
				if (!isChange)
				{
					Debug.LogError("收纳盒出现了重复的物件" + baseItem.archiveData.index + " " + baseItem.itemData.id + " " + item.itemData.id);
				}
				RemoveItem(item, false);
			}
			AddItem(baseItem);
			return baseItem;
		}

		public void AttachItemToLayer(BaseItem item, int layer)
		{
			if (layer == 9)
			{
				item.SetLayer(layer, 0);
				SetItemParent(item);
				storageBox.AddItem(item);
			}
			else
			{
				item.SetLayer(layer, mapData.id);
				SetItemParent(item);
			}
		}

		public void SetItemParent(BaseItem item)
		{
			if (item.archiveData.layer == 1)
			{
				item.transform.SetParent(map.distLayerItem);
				if (item.itemData.scriptType == 5)
				{
					depthManager.AddToDistLayer(item.depthSystem, DepthType.Front);
				}
				else
				{
					depthManager.AddToDistLayer(item.depthSystem, DepthType.Ahead);
				}
			}
			else if (item.archiveData.layer == 9)
			{
				item.transform.SetParent(storageBox.itemTrans);
				depthManager.AddToStorageBoxLayer(item.depthSystem, DepthType.Front);
			}
			else
			{
				item.transform.SetParent(map.sceneLayerItem);
				depthManager.AddToSceneLayer(item.depthSystem, DepthType.Behind);
			}
		}

		public void Update()
		{
			if (!isGameStart)
			{
				return;
			}
			if (canTouch)
			{
				if (ClientConfig.PLATFORM == PlatformType.Editor)
				{
					if (Input.GetMouseButtonDown(0))
					{
						SimulateTouch(0, Input.mousePosition);
					}
					else if (Input.GetMouseButton(0))
					{
						SimulateTouch(1, Input.mousePosition);
					}
					else if (Input.GetMouseButtonUp(0))
					{
						SimulateTouch(2, Input.mousePosition);
					}
				}
				else if (Input.touchCount > 0 && Input.touchCount < 12)
				{
					for (int i = 0; i < Input.touchCount; i++)
					{
						touch = Input.GetTouch(i);
						if (touch.phase == TouchPhase.Began)
						{
							TouchDown(touch);
						}
						else if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary)
						{
							TouchMove(touch);
						}
						else if (touch.phase == TouchPhase.Ended)
						{
							TouchUp(touch);
						}
					}
				}
			}
			audioManager.UpdateAudioPlayers(cameraManager.GetPosition().x);
		}

		public void LateUpdate()
		{
			if (isGameStart)
			{
				if (storageBox != null)
				{
					storageBox.LatedUpdate();
				}
				LateUpdateStorageBoxMove();
				LateUpdateStorageBoxCameraMove();
				LateUpdateCameraMove();
			}
		}

		private void LateUpdateStorageBoxMove()
		{
			if (sbMoveType == StorageBoxMoveType.Static)
			{
				return;
			}
			if (sbMoveType == StorageBoxMoveType.Follow)
			{
				if (storageBox.GetBoxY() > targetSBY)
				{
					sbMoveV = (0f - Mathf.Abs(storageBox.GetBoxY() - targetSBY)) / 0.03f;
				}
				else if (storageBox.GetBoxY() < targetSBY)
				{
					sbMoveV = Mathf.Abs(storageBox.GetBoxY() - targetSBY) / 0.03f;
				}
				else
				{
					sbMoveV = 0f;
				}
				storageBox.SetBoxY(storageBox.GetBoxY() + sbMoveV * Time.deltaTime);
				if (sbMoveV > 0f)
				{
					if (storageBox.GetBoxY() > targetSBY)
					{
						storageBox.SetBoxY(targetSBY);
						sbMoveType = StorageBoxMoveType.Static;
					}
				}
				else if (sbMoveV < 0f && storageBox.GetBoxY() < targetSBY)
				{
					storageBox.SetBoxY(targetSBY);
					sbMoveType = StorageBoxMoveType.Static;
				}
				if (sbMoveV == 0f || storageBox.GetBoxY() == targetSBY)
				{
					sbMoveType = StorageBoxMoveType.Static;
				}
			}
			else if (sbMoveType == StorageBoxMoveType.Elastic)
			{
				if (Mathf.Abs(storageBox.GetBoxY() - targetSBY) > 0.005f)
				{
					storageBox.SetBoxY(Mathf.Lerp(storageBox.GetBoxY(), targetSBY, Time.deltaTime * 5f));
					return;
				}
				storageBox.SetBoxY(targetSBY);
				sbMoveType = StorageBoxMoveType.Static;
			}
		}

		private void LateUpdateStorageBoxCameraMove()
		{
			if (sbCameraMoveType == StorageBoxCameraMoveType.Static)
			{
				return;
			}
			if (sbCameraMoveType == StorageBoxCameraMoveType.MoveLeft)
			{
				storageBox.difValX += 4f * Time.deltaTime;
				if (storageBox.difValX >= 2f)
				{
					storageBox.difValX = 2f;
					sbCameraMoveType = StorageBoxCameraMoveType.Static;
					storageBox.SetFloorColliderEnabled(true);
				}
			}
			else if (sbCameraMoveType == StorageBoxCameraMoveType.MoveRight)
			{
				storageBox.difValX -= 4f * Time.deltaTime;
				if (storageBox.difValX <= 0f)
				{
					storageBox.difValX = 0f;
					sbCameraMoveType = StorageBoxCameraMoveType.Static;
					storageBox.SetFloorVisible(false);
				}
			}
			storageBox.SetPositionX(cameraManager.cameraTrans.position.x);
		}

		private void LateUpdateCameraMove()
		{
			if (cameraMoveType == CameraMoveType.Static)
			{
				return;
			}
			if (cameraMoveType == CameraMoveType.Follow)
			{
				if (cameraManager.GetPosition().x > targetCameraX)
				{
					cameraMoveV = (0f - Mathf.Abs(cameraManager.GetPosition().x - targetCameraX)) / 0.03f;
				}
				else if (cameraManager.GetPosition().x < targetCameraX)
				{
					cameraMoveV = Mathf.Abs(cameraManager.GetPosition().x - targetCameraX) / 0.03f;
				}
				else
				{
					cameraMoveV = 0f;
				}
				cameraManager.SetPositionX(cameraManager.GetPosition().x + cameraMoveV * Time.deltaTime);
				if (cameraMoveV > 0f)
				{
					if (cameraManager.GetPosition().x > targetCameraX)
					{
						cameraManager.SetPositionX(targetCameraX);
						cameraMoveType = CameraMoveType.Static;
					}
				}
				else if (cameraMoveV < 0f)
				{
					if (cameraManager.GetPosition().x < targetCameraX)
					{
						cameraManager.SetPositionX(targetCameraX);
						cameraMoveType = CameraMoveType.Static;
					}
				}
				else if (cameraMoveV == 0f || cameraManager.GetPosition().x == targetCameraX)
				{
					cameraMoveType = CameraMoveType.Static;
				}
			}
			else if (cameraMoveType == CameraMoveType.Interia)
			{
				cameraMoveV -= cameraMoveV * Time.deltaTime * 2f;
				if (Mathf.Abs(cameraMoveV) < 14f)
				{
					if (Mathf.Abs(cameraMoveV) < 2f)
					{
						cameraMoveV = 0f;
						cameraMoveType = CameraMoveType.Static;
						return;
					}
					cameraMoveV -= ((2f * cameraMoveV > 0f) ? 1 : (-1));
				}
				cameraManager.SetPositionX(cameraManager.GetPosition().x + cameraMoveV * Time.deltaTime);
			}
			else if (cameraMoveType == CameraMoveType.Uniform)
			{
				cameraManager.SetPositionX(cameraManager.GetPosition().x + cameraMoveV * Time.deltaTime);
			}
		}

		public void SimulateTouch(int type, Vector2 position)
		{
			switch (type)
			{
			case 0:
				TouchDown(NewTouch(TouchPhase.Began, position));
				break;
			case 1:
				TouchMove(NewTouch(TouchPhase.Moved, position));
				break;
			case 2:
				TouchUp(NewTouch(TouchPhase.Ended, position));
				break;
			}
		}

		private Touch NewTouch(TouchPhase phase, Vector3 pos)
		{
			Touch result = default(Touch);
			result.fingerId = 0;
			result.phase = phase;
			result.position = pos;
			return result;
		}

		private void TouchDown(Touch touch)
		{
			if (touch.fingerId > 11)
			{
				return;
			}
			if (clrCameraTouchId >= 0 || clrStorageBoxTouchId >= 0)
			{
				isClrCameraTouchExist = false;
				isClrStorageBoxTouchExist = false;
				for (int i = 0; i < Input.touches.Length; i++)
				{
					if (Input.touches[i].fingerId != touch.fingerId)
					{
						if (Input.touches[i].fingerId == clrCameraTouchId)
						{
							isClrCameraTouchExist = true;
						}
						if (Input.touches[i].fingerId == clrStorageBoxTouchId)
						{
							isClrStorageBoxTouchExist = true;
						}
					}
				}
				if (!isClrCameraTouchExist)
				{
					clrCameraTouchId = -1;
				}
				if (!isClrStorageBoxTouchExist)
				{
					clrStorageBoxTouchId = -1;
				}
			}
			TouchEventData touchEventData = touchEvtDatas[touch.fingerId];
			touchEventData.Init(uiManager.IsPointerOverUI(touch.position));
			touchEventData.curPos = cameraManager.camera.ScreenToWorldPoint(touch.position);
			touchEventData.lastTouchPos = touch.position;
			if (canPlay)
			{
				if (!touchEventData.isClickUI && !TouchDownStorageBoxCheck(touch, touchEventData))
				{
					TouchDownItemCheck(touch, touchEventData);
				}
			}
			else
			{
				CheckTouchClrCamera(touchEventData);
			}
		}

		private bool TouchDownStorageBoxCheck(Touch touch, TouchEventData touchEvtData)
		{
			hit = Physics2D.Raycast(touchEvtData.curPos, Vector2.zero, 15f, 16384);
			if (hit.collider != null)
			{
				if (hit.collider.gameObject.name.Equals("Close"))
				{
					touchEvtData.isClickSBBtn = true;
					sbCameraMoveType = StorageBoxCameraMoveType.MoveRight;
					storageBox.SetFloorColliderEnabled(false);
					storageBox.IsOpen(false);
					isSBOpen = false;
					Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
					return true;
				}
				if (hit.collider.gameObject.name.Equals("Open"))
				{
					touchEvtData.isClickSBBtn = true;
					sbCameraMoveType = StorageBoxCameraMoveType.MoveLeft;
					storageBox.SetFloorVisible(true);
					storageBox.IsOpen(true);
					isSBOpen = true;
					Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
					return true;
				}
				if (hit.collider.gameObject.name.Equals("BtnLock"))
				{
					touchEvtData.isClickSBBtn = true;
					Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
					clr.OCPurchaseView(true, true);
					return true;
				}
			}
			hit = Physics2D.Raycast(touchEvtData.curPos, Vector2.zero, 15f, 8192);
			if (hit.collider != null)
			{
				touchEvtData.inSBArea = true;
				return false;
			}
			return false;
		}

		private void TouchDownItemCheck(Touch touch, TouchEventData touchEvtData)
		{
			hits = Physics2D.RaycastAll(touchEvtData.curPos, Vector2.zero, 15f, 512);
			BaseItem baseItem = null;
			List<BaseItem> list = new List<BaseItem>();
			for (int i = 0; i < hits.Length; i++)
			{
				hit = hits[i];
				if (!(hit.collider != null))
				{
					continue;
				}
				baseItem = hit.collider.transform.parent.GetComponent<BaseItem>();
				if (baseItem.CanTouch())
				{
					if (baseItem.archiveData.layer != 9 || baseItem.itemData.scriptType == 1)
					{
						break;
					}
					baseItem = null;
				}
				else
				{
					list.Add(baseItem);
					baseItem = null;
				}
			}
			if (baseItem == null)
			{
				for (int j = 0; j < list.Count; j++)
				{
					if (list[j] != null)
					{
						baseItem = list[j];
						break;
					}
				}
			}
			list.Clear();
			if (baseItem != null)
			{
				if ((baseItem.archiveData.layer == 9 && baseItem.itemData.scriptType != 1) || (touchEvtData.inSBArea && baseItem.archiveData.layer != 9))
				{
					baseItem = null;
				}
				if (baseItem != null)
				{
					if (isLock && baseItem.archiveData.layer == 9)
					{
						baseItem = null;
					}
					else
					{
						baseItem.TouchDown(touchEvtData);
						touchEvtData.selectItem = baseItem;
					}
				}
			}
			if (touchEvtData.inSBArea)
			{
				if (clrStorageBoxTouchId < 0 || clrStorageBoxTouchId == touchEvtData.fingerId)
				{
					clrStorageBoxTouchId = touch.fingerId;
					targetSBY = storageBox.GetBoxY();
				}
			}
			else
			{
				CheckTouchClrCamera(touchEvtData);
			}
		}

		private void CheckTouchClrCamera(TouchEventData touchEvtData)
		{
			if (CheckTouchCanClrCamera(touchEvtData))
			{
				if (clrCameraTouchId < 0 || clrCameraTouchId == touchEvtData.fingerId)
				{
					touchEvtData.canClrCameraMove = true;
					clrCameraTouchId = touch.fingerId;
					targetCameraX = cameraManager.GetPosition().x;
					cameraMoveType = CameraMoveType.Follow;
				}
			}
			else
			{
				touchEvtData.canClrCameraMove = false;
			}
		}

		private void TouchMove(Touch touch)
		{
			if (touch.fingerId > 11)
			{
				return;
			}
			TouchEventData touchEventData = touchEvtDatas[touch.fingerId];
			touchEventData.curPos = cameraManager.camera.ScreenToWorldPoint(touch.position);
			if (canPlay)
			{
				if (!touchEventData.isClickUI)
				{
					TouchMoveStorageBoxCheck(touch, touchEventData);
					TouchMoveItemCheck(touch, touchEventData);
				}
			}
			else
			{
				TouchMoveItemCheck(touch, touchEventData);
			}
		}

		private void TouchMoveStorageBoxCheck(Touch touch, TouchEventData touchEventData)
		{
			if (clrStorageBoxTouchId == touch.fingerId)
			{
				targetSBY = storageBox.CalBoxMoveY(targetSBY + (touch.position.y - touchEventData.lastTouchPos.y) * cameraManager.pixelWSize);
				sbMoveType = StorageBoxMoveType.Follow;
				touchEventData.lastTouchPos = touch.position;
			}
		}

		private void TouchMoveItemCheck(Touch touch, TouchEventData touchEvtData)
		{
			if (touchEvtData.selectItem != null)
			{
				if (touchEvtData.inSBArea)
				{
					hit = Physics2D.Raycast(touchEvtData.curPos, Vector2.zero, 15f, 8192);
					if (hit.collider == null || hit.collider.transform.name.Equals("Floor"))
					{
						touchEvtData.inSBArea = false;
						AttachItemToLayer(touchEvtData.selectItem, 0);
						storageBox.RemoveItem(touchEvtData.selectItem);
						ReleaseClrStorageBoxTouch(touch.fingerId);
					}
				}
				else
				{
					touchEvtData.selectItem.TouchMove(touchEvtData);
					if (touchEvtData.selectItem.itemData.canDrag && (clrCameraTouchId < 0 || clrCameraTouchId == touch.fingerId))
					{
						if (touch.position.x <= cameraMoveMinArea)
						{
							cameraMoveV = (touch.position.x - cameraMoveMinArea) / 25f;
							UniformMoveCamera(touch.fingerId);
						}
						else if (!isSBOpen && touch.position.x >= cameraMoveMaxArea)
						{
							cameraMoveV = (cameraMoveMinArea - ((float)Screen.width - touch.position.x)) / 25f;
							UniformMoveCamera(touch.fingerId);
						}
						else
						{
							clrCameraTouchId = -1;
							cameraMoveType = CameraMoveType.Static;
						}
					}
				}
			}
			if (touchEvtData.canClrCameraMove)
			{
				targetCameraX = cameraManager.CalculateX(targetCameraX - (touch.position.x - touchEvtData.lastTouchPos.x) * cameraManager.pixelWSize);
				cameraMoveType = CameraMoveType.Follow;
				touchEvtData.lastTouchPos = touch.position;
			}
		}

		private void UniformMoveCamera(int fingerId)
		{
			clrCameraTouchId = fingerId;
			cameraMoveType = CameraMoveType.Uniform;
		}

		private bool CheckTouchCanClrCamera(TouchEventData touchEventData)
		{
			if (!touchEventData.inSBArea && !touchEventData.isClickSBBtn)
			{
				if (touchEventData.selectItem == null)
				{
					return true;
				}
				ItemData itemData = touchEventData.selectItem.itemData;
				if (!itemData.canDrag && !itemData.lockCamera)
				{
					return true;
				}
			}
			return false;
		}

		private void TouchUp(Touch touch)
		{
			if (touch.fingerId > 11)
			{
				return;
			}
			TouchEventData touchEventData = touchEvtDatas[touch.fingerId];
			touchEventData.curPos = cameraManager.camera.ScreenToWorldPoint(touch.position);
			if (canPlay)
			{
				if (!touchEventData.isClickUI)
				{
					TouchUpStorageBoxCheck(touch, touchEventData);
					TouchUpItemCheck(touch, touchEventData);
				}
			}
			else
			{
				TouchUpItemCheck(touch, touchEventData);
			}
		}

		private void TouchUpStorageBoxCheck(Touch touch, TouchEventData touchEventData)
		{
			ReleaseClrStorageBoxTouch(touch.fingerId);
		}

		private void ReleaseClrStorageBoxTouch(int figureId)
		{
			if (clrStorageBoxTouchId == figureId)
			{
				clrStorageBoxTouchId = -1;
				targetSBY = storageBox.CalBoxStaticY(targetSBY);
				sbMoveType = StorageBoxMoveType.Elastic;
			}
		}

		private void TouchUpItemCheck(Touch touch, TouchEventData touchEvtData)
		{
			if (clrCameraTouchId == touchEvtData.fingerId)
			{
				clrCameraTouchId = -1;
				if (touchEvtData.canClrCameraMove)
				{
					if (Mathf.Abs(cameraMoveV) > 15f)
					{
						cameraMoveType = CameraMoveType.Interia;
					}
					else
					{
						targetCameraX = cameraManager.CalculateX(targetCameraX - (touch.position.x - touchEvtData.lastTouchPos.x) * cameraManager.pixelWSize);
						cameraMoveType = CameraMoveType.Follow;
					}
				}
				else
				{
					cameraMoveType = CameraMoveType.Static;
				}
			}
			if (!(touchEvtData.selectItem != null))
			{
				return;
			}
			if (!touchEvtData.selectItem.itemData.canDrag)
			{
				hit = Physics2D.Raycast(touchEvtData.curPos, Vector2.zero, 15f, 512);
				if (hit.collider != null)
				{
					BaseItem component = hit.collider.transform.parent.GetComponent<BaseItem>();
					if (component == touchEvtData.selectItem)
					{
						component.TouchUpOnSameItem(touchEvtData);
					}
				}
			}
			touchEvtData.selectItem.TouchUp(touchEvtData);
			if (touchEvtData.inSBArea && touchEvtData.selectItem.archiveData.layer != 9)
			{
				storageBox.RemoveItem(touchEvtData.selectItem);
			}
			touchEvtData.selectItem = null;
		}

		public void ChangeBrush(string id)
		{
			List<BaseItem> itemsByScriptType = GetItemsByScriptType(3);
			Color32 paintColor = default(Color32);
			for (int i = 0; i < itemsByScriptType.Count; i++)
			{
				BrushItem brushItem = (BrushItem)itemsByScriptType[i];
				if (brushItem.archiveData.itemData.id.Equals(id))
				{
					brushItem.SetState(BrushItem.State.Up);
					paintColor = brushItem.paintColor;
				}
				else
				{
					brushItem.SetState(BrushItem.State.Default);
				}
			}
			BaseItem itemByScriptType = GetItemByScriptType(2);
			if (itemByScriptType != null)
			{
				((PaintBoardItem)itemByScriptType).paintColor = paintColor;
			}
		}

		public void ChangeSunLight(float percent)
		{
			sunPercent = percent;
			ResetLight();
			if (percent >= 1f && star != null)
			{
				star.SetStarVisible(false);
			}
		}

		public void OpenLamp(uint index, float val)
		{
			if (!openLamps.ContainsKey(index))
			{
				openLamps.Add(index, val);
				ChangeLight(val);
			}
		}

		public void CloseLamp(uint index)
		{
			if (openLamps.ContainsKey(index))
			{
				ChangeLight(0f - openLamps[index]);
				openLamps.Remove(index);
			}
		}

		public void ChangeLight(float val)
		{
			lampLight += val;
			ResetLight();
		}

		public void ResetLight()
		{
			light.intensity = mapData.basicLight + mapData.sunLight * sunPercent + lampLight;
		}

		public void ChangeSunState(bool isSun)
		{
			SetSunMoonEnviroment(isSun);
		}

		private void SetSunMoonEnviroment(bool isSun)
		{
			if (!isSun && star != null)
			{
				star.InitStars();
				star.SetStarVisible(true);
			}
		}

		public void OnCameraChange(float percent)
		{
			if (storageBox != null)
			{
				storageBox.SetPositionX(cameraManager.cameraTrans.position.x);
			}
			if (map != null)
			{
				map.UpdateLayerPosition(percent);
			}
		}

		public void StopGame()
		{
			isGameStart = false;
		}

		public int GetMapId()
		{
			if (map != null)
			{
				return map.mapId;
			}
			return 0;
		}

		public void ReleaseAllTouch()
		{
			for (int i = 0; i < Input.touchCount; i++)
			{
				touch = Input.GetTouch(i);
				TouchUp(touch);
			}
		}

		public void SaveMapArchive()
		{
			if (isGameStart && items.Count > 0)
			{
				model.SaveArchiveData(map.mapId, items, cameraManager.GetPosition().x, model.maxItemIndex);
			}
		}

		public BaseItem GetItem(uint index)
		{
			for (int i = 0; i < items.Count; i++)
			{
				BaseItem baseItem = items[i];
				if (baseItem.archiveData.index == index)
				{
					return baseItem;
				}
			}
			return null;
		}

		public BaseItem GetItemById(string itemId)
		{
			for (int i = 0; i < items.Count; i++)
			{
				BaseItem baseItem = items[i];
				if (baseItem.itemData.id.Equals(itemId))
				{
					return baseItem;
				}
			}
			return null;
		}

		public BaseItem GetItemByIdAndLayer(string itemId, int layer)
		{
			for (int i = 0; i < items.Count; i++)
			{
				BaseItem baseItem = items[i];
				if (baseItem.itemData.id.Equals(itemId) && baseItem.itemData.layer == layer)
				{
					return baseItem;
				}
			}
			return null;
		}

		public List<BaseItem> GetItemByType(int type)
		{
			List<BaseItem> list = new List<BaseItem>();
			for (int i = 0; i < items.Count; i++)
			{
				BaseItem baseItem = items[i];
				if (baseItem.itemData.type == type)
				{
					list.Add(baseItem);
				}
			}
			return list;
		}

		public BaseItem GetItemByScriptType(int type)
		{
			for (int i = 0; i < items.Count; i++)
			{
				BaseItem baseItem = items[i];
				if (baseItem.itemData.scriptType == type)
				{
					return baseItem;
				}
			}
			return null;
		}

		public List<BaseItem> GetItemsByScriptType(int type)
		{
			List<BaseItem> list = new List<BaseItem>();
			for (int i = 0; i < items.Count; i++)
			{
				BaseItem baseItem = items[i];
				if (baseItem.itemData.scriptType == type)
				{
					list.Add(baseItem);
				}
			}
			return list;
		}

		public void AddItem(BaseItem item)
		{
			items.Add(item);
		}

		public bool RemoveItemByIndex(uint index, bool removeRelationship)
		{
			return RemoveItem(GetItem(index), removeRelationship);
		}

		public bool RemoveItem(BaseItem item, bool removeRelationship)
		{
			if (item != null)
			{
				if (removeRelationship)
				{
					for (int i = 0; i < item.childItems.Count; i++)
					{
						RemoveItem(item.childItems[i], removeRelationship);
					}
					if (item.archiveData.createParentIndex != 0)
					{
						BaseItem item2 = GetItem(item.archiveData.createParentIndex);
						if (item2 != null)
						{
							item2.RemoveCreateItem(item);
						}
						else
						{
							model.CheckRedundantData(map.mapId, item.archiveData.createParentIndex, RedundantData.DataType.CreateIndex, item.archiveData.index);
						}
					}
					for (int j = 0; j < item.archiveData.createIndexs.Count; j++)
					{
						BaseItem item3 = GetItem(item.archiveData.createIndexs[j]);
						if (item3 != null)
						{
							item.RemoveCreateItem(item3);
						}
						else
						{
							model.CheckRedundantData(map.mapId, item.archiveData.createIndexs[j], RedundantData.DataType.CreateParentIndex, item.archiveData.index);
						}
					}
				}
				items.Remove(item);
				item.DestroySelf();
				return true;
			}
			return false;
		}

		public void CanTouch(bool val)
		{
			canTouch = val;
		}

		public void ClearScene()
		{
			audioManager.Clear();
			for (int i = 0; i < items.Count; i++)
			{
				if (items[i] != null)
				{
					Object.Destroy(items[i].gameObject);
				}
			}
			items.Clear();
			if (map != null)
			{
				map.ClearMap();
				map = null;
			}
			if (storageBox != null)
			{
				storageBox.Clear();
				storageBox = null;
			}
			InitData();
			Singleton<GamePoolManager>.Instance.ClearPerfabPool();
		}
	}
}
